package org.jeecg.modules.crm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.jeecg.modules.crm.entity.SerialNumber;
import org.jeecg.modules.crm.mapper.SerialNumberMapper;
import org.jeecg.modules.crm.service.ISerialNumberService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * @Description: serial number
 * @Author: jeecg-boot
 * @Date:   2025-04-28
 * @Version: V1.0
 */
@Service
public class SerialNumberServiceImpl extends ServiceImpl<SerialNumberMapper, SerialNumber> implements ISerialNumberService {

    @Override
    public String queryBytype(String type) {
        QueryWrapper<SerialNumber> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type", type);
        SerialNumber serialNumber = baseMapper.selectOne(queryWrapper);
        return generateSerialNumber(serialNumber.getNumber());
    }

    /***
     * 修改流水号
     * @param type
     * @return
     */

    @Override
    public String updatebytype(String type) {

        return null;
    }

    public String generateSerialNumber(String digitCountStr) {
        if ("0".equals(digitCountStr)) {
            return "0001";
        }
        try {
            int digitCount = Integer.parseInt(digitCountStr);
            if (digitCount < 1) {
                throw new IllegalArgumentException("传入的参数必须是大于 0 的整数");
            }
            String format = "%" + "0" + digitCount + "d";
            return String.format(format, 1);
        } catch (NumberFormatException e) {
            throw new IllegalArgumentException("传入的参数必须是有效的整数", e);
        }
    }
}
